Ext.chloroform.ScenarioPropertyGrid = Ext.extend(Ext.grid.PropertyGrid, {

    /**
     * @cfg {Object} the store containing cookies for pages
     */
    cookiesForPageStore: null,
    /**
     * @cfg {Object} the combo containing cookies for the selected entry (page)
     */
    cookiesForPageCombo: null,

    margins: '5 0 0 0',
    cmargins: '5 5 0 0',
    padding: '0 0 0 0',

    autoHeight: true,
    source: {},
    viewConfig : {
        forceFit: true
        //scrollOffset: 2 // the grid will never have scroll-bars
    },

    cookiesModel: null,
    pageName: null,
    cookieIndexForPage: {},
    cachedCustomEditors: {},

    getCookieIndex: function() {
        return this.cookieIndexForPage[this.pageName];
    },

    onEditAllPreConfigCookies: function(message, data) {
        this.cookiesModel = message;
        this.cachedCustomEditors = {};
    },

    selectCookie: function(cookieIndex) {
        this.cookieIndexForPage[this.pageName] = cookieIndex;

        var pageCookies = this.cookiesModel.entries[this.pageName].cookies;
        var pageCookie = pageCookies[cookieIndex];
        var cookieFields = pageCookie.fields;

        var newSource = {};

        // Caching the cell editors removes the problem of not being possible to put
        // the cells in edit mode.
        var editorsForPage = this.cachedCustomEditors[this.pageName + cookieIndex];
        var buildEditors = !editorsForPage;
        if (buildEditors) {
            editorsForPage = {};
        }

        var renderersForPage = {};

        for (var j = 0; j < cookieFields.length; j++) {
            var field = cookieFields[j];
            newSource[field.name] = pageCookie.fieldsObject[field.name].value;
            if (buildEditors) {
                editorsForPage[field.name] = new Ext.grid.GridEditor(field);
            }

            if (field.customRenderer) {
                renderersForPage[field.name] = field.customRenderer;
            }
        }

        this.cachedCustomEditors[this.pageName + cookieIndex] = editorsForPage;

        this.setSource(newSource);
        this.customEditors = editorsForPage;
        this.customRenderers = renderersForPage;
    },

    selectPage: function(pageName) {
        this.pageName = pageName;

        this.cookiesForPageStore.removeAll(false);

        if (this.cookiesModel != null) {
            var pageCookies = this.cookiesModel.entries[pageName].cookies;
            if (pageCookies.length == 0) {
                this.setSource({});
                this.cookiesForPageCombo.clearValue();
                this.doLayout();
            } else {
                for (var i = 0; i < pageCookies.length; i++) {
                    this.cookiesForPageStore.add(new this.cookiesForPageStore.recordType({ index: i, pageId: 'Cookie ' + i }, i));
                }

                var cookieIndexForPage = this.cookieIndexForPage[this.pageName];
                if (!cookieIndexForPage) cookieIndexForPage = 0;

                this.cookiesForPageCombo.setValue(cookieIndexForPage);
                this.selectCookie(cookieIndexForPage);

            }

        }
    }

});
